package timing.ukulele.minio.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.minio.persistent.MetadataInfoEntity;
import timing.ukulele.minio.repository.MetadataRepository;

@Slf4j
@RestController
@RequestMapping("/file")
@Tag(name = "文件接口")
public class FileController {
    /**
     * 文件元数据服务接口定义
     */
    private final MetadataRepository fileMetadataRepository;

    public FileController(MetadataRepository fileMetadataRepository) {
        this.fileMetadataRepository = fileMetadataRepository;
    }

    @Operation(summary = "文件分页列表接口", description = "文件分页列表")
    @GetMapping("/page")
    public ResponseData<Page<MetadataInfoEntity>> page(@RequestParam(value = "name", required = false) String name, @RequestParam("current") Long current, @RequestParam("pageSize") Long size) {
        Page<MetadataInfoEntity> filter = new Page<>(current, size);
        LambdaQueryChainWrapper<MetadataInfoEntity> query = this.fileMetadataRepository.lambdaQuery();
        if (StringUtils.hasText(name))
            query.like(MetadataInfoEntity::getFileName, name);
        query.orderByDesc(MetadataInfoEntity::getId);
        Page<MetadataInfoEntity> page = query.page(filter);;
        return ResponseData.success(page);
    }
}
